﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using System.Text;
using SmartSpace.UI.Silverlight.Dynamic.Controls.Window;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    /// <summary>
    /// 选择用户控件
    /// </summary>
    public partial class UserListControl : PMFDataControl
    {
        private string m_username;

        public UserListControl(string username)
        {
            m_username = username;

            InitializeComponent();
        }

        #region DependencyProperty

        public User SelectedUser
        {
            get { return (User)GetValue(SelectedUserProperty); }
            set { SetValue(SelectedUserProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedUser.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedUserProperty =
            DependencyProperty.Register("SelectedUser", typeof(User), typeof(UserListControl), new PropertyMetadata(default(User), OnSelectedUserPropertyChangedCallback));

        static void OnSelectedUserPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UserListControl ctl = d as UserListControl;
            ctl.UpdateSelectedText();
        }

        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(UserListControl), new PropertyMetadata(default(bool), OnIsReadOnlyPropertyChangedCallback));

        static void OnIsReadOnlyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            UserListControl ctrl = d as UserListControl;
            ctrl.SetReadOnly((bool)e.NewValue);
        }

        #endregion

        #region Overrides

        public override void UpdateSource()
        {
            this.GetBindingExpression(UserListControl.SelectedUserProperty).UpdateSource();
        }

        private void _ctlMessage_MouseEnter(object sender, MouseEventArgs e)
        {
            _ctlMessage.Visibility = System.Windows.Visibility.Collapsed;
        }

        #endregion

        private void SetReadOnly(bool isReadOnly)
        {
            if (isReadOnly)
            {
                m_btnBrowser.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                m_btnBrowser.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void OnButtonBrowserClick(object sender, RoutedEventArgs e)
        {
            SelectUserWindow window = new SelectUserWindow(m_username);
            window.InitSelectedUser(this.SelectedUser);
            window.Closed += new EventHandler(OnWindowClosed);
            window.Show();
        }

        void OnWindowClosed(object sender, EventArgs e)
        {
            SelectUserWindow window = sender as SelectUserWindow;
            if (window.DialogResult == true)
            {
                SelectedUser = window.SelectedUser;
            }
        }

        /// <summary>
        /// 获取新值后更新控件显示
        /// </summary>
        private void UpdateSelectedText()
        {
            StringBuilder textBuilder = new StringBuilder();
            if (this.SelectedUser != null)
            {
                textBuilder.Append(this.SelectedUser.Caption);
            }
            else
            {
                textBuilder.Append("请选择");
            }

            m_textUserName.Text = textBuilder.ToString();
        }

    }
}
